package com.gxuwz.medical.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.member.*;
public class MemberDao extends GenericDao<Member>{

	@Override
	protected Member handle(ResultSet rs) throws SQLException {
		String nhzcode = rs.getString("nhzcode");
		String ylzcode = rs.getString("ylzcode");
		String familycode = rs.getString("familycode");
		String housecode = rs.getString("housecode");
		String relation = rs.getString("relation");
		String idcard = rs.getString("idcard");
		String name = rs.getString("name");
		
		String sex = rs.getString("sex");
		String birthday = rs.getString("birthday");
		String nation = rs.getString("nation");
		String health = rs.getString("health");
		String culture = rs.getString("culture");
		String farmer = rs.getString("farmer");
		String work = rs.getString("work");
		String phone = rs.getString("phone");
		String age = rs.getString("age");
		String occupation = rs.getString("occupation");
		
		Member entity = new Member(nhzcode,ylzcode,familycode,housecode,relation,idcard,name,sex,birthday,nation,health,culture,farmer,work,phone,age,occupation);
		return entity;
	}
	
	// 通过家庭编号自动计算家庭人口数
		 public int getMemberSizeById(String familycode) throws Exception {
				Connection conn = null;
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				int familySize = 0;
				try{
					conn = DbUtil.getConn();
					StringBuffer sql = new StringBuffer("select count(*) from t_member where familycode=? ");
					pstmt = conn.prepareStatement(sql.toString());
					int index = 1;
					pstmt.setString(index, familycode);
					rs = pstmt.executeQuery();
					if(rs.next()){
						familySize = rs.getInt(1);
					}
				}catch(SQLException e){
					throw new SQLException("Member with id " + familycode + " could not be loaded from the database.", e);
				}finally{
					DbUtil.close(rs, pstmt, conn);
				}
				return familySize;
			}
		 
		 /**
			 * 根据姓名模糊查询个人记录，考虑同名的情况
			 * @param name
			 * @param areacode
			 * @return
			 * @throws SQLException
			 */
			public List<Member> findByKeyWord(String name,String areacode)throws Exception{
				try{
					String sql="select * from t_member where familycode like'"
				             +areacode+"%' and name like'%"+name+"%'";
				    Object[]params=null;
//				    System.out.println("sql模糊查询语句："+sql);
					List<Member> persons=queryOjects(sql, params);
					return persons;
				}catch(Exception e){
					e.printStackTrace();
					throw e;
				}
			}

}
